//
//  ST_ConversationListCell.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/18.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import SwiftyJSON
import SwifterSwift

class ST_ConversationListCell: UITableViewCell {


    static let height = 78.cgFloat
//    MARK:- init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        self.selectionStyle = .none
        
        self.contentView.addSubview(user_image_view)
        self.contentView.addSubview(icon_supplement_image_view)
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(user_image_view)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(icon_supplement_image_view.snp.height)
        }
        
        self.contentView.addSubview(nickname_label)
        self.contentView.addSubview(time_label)
        self.contentView.addSubview(last_msg_label)
        
        
        let userImageHeight = 58.cgFloat
        user_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(15)
            make.width.height.equalTo(userImageHeight)
            make.centerY.equalToSuperview()
        }
        
        nickname_label.snp.makeConstraints { (make) in
            make.left.equalTo(self.user_image_view.snp.right).offset(10)
            make.top.equalTo(self.user_image_view).offset(5)
            make.height.equalTo(25)
            if ScreenWidth < 400{
                make.width.lessThanOrEqualTo(100)
            }else if ScreenWidth < 420{
                make.width.lessThanOrEqualTo(138)
            }
        }
        
        /*
        contentView.addSubview(sign_image_container)
        sign_image_container.snp.makeConstraints { (make) in
            make.left.equalTo(nickname_label.snp.right).offset(4)
            make.centerY.equalTo(nickname_label).offset(-1)
            make.height.equalTo(18)
            make.width.equalTo(70)
        }*/
        
        contentView.addSubview(conversation_guide_image_view)
        conversation_guide_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(nickname_label.snp.right).offset(4)
            make.centerY.equalTo(nickname_label).offset(-1)
        }
        
        contentView.addSubview(online_sub_label)
        online_sub_label.snp.makeConstraints { (make) in
            make.left.equalTo(conversation_guide_image_view.snp.right).offset(4)
            make.centerY.equalTo(nickname_label).offset(-1)
        }
        
        
        contentView.addSubview(nobility_image_view)
        nobility_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(online_sub_label.snp.right).offset(4)
            make.centerY.equalTo(online_sub_label)
            make.width.equalTo(18)
            make.height.equalTo(18)
        }
        

        last_msg_label.snp.makeConstraints { (make) in
            make.left.equalTo(nickname_label)
            make.right.equalToSuperview().offset(-43)
            make.bottom.equalToSuperview().offset(-15)
            make.height.equalTo(20)
        }

        time_label.snp.makeConstraints { (make) in
            
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(nickname_label)
        }
        
        self.contentView.addSubview(un_read_count_label)
        un_read_count_label.snp.makeConstraints { (make) in
            make.centerY.equalTo(last_msg_label)
            make.right.equalTo(self.contentView).offset(-15)
            make.height.equalTo(18)
            make.width.greaterThanOrEqualTo(18)
        }
        
        self.contentView.addSubview(sweet_sign_image_view)
        sweet_sign_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(time_label)
            make.right.equalTo(self.time_label.snp.left).offset(-5)
            make.width.height.equalTo(19)
        }
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    MARK:- Properties
    lazy var user_image_view:UIImageView = {
        
        let control = UIImageView()
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 58/2.0
//        control.borderColor = Color.init(hexString: "#F2F4F7")
//        control.borderWidth = 2
        
        return control
    }()

    fileprivate lazy var icon_supplement_image_view:UIImageView = {
        let control = UIImageView()
       control.contentMode = UIView.ContentMode.scaleAspectFill
       return control
    }()

    
    lazy var nickname_label:UILabel = {
       
        let control = UILabel()
        
        control.textColor = Color.init(hexString: "#333333")!
        
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        return control
    }()
    
    
//    private var goddessNicknameLabel:ST_ColoryLabel?
    
    
    lazy var last_msg_label:UILabel = {
        
        let control = UILabel()
        
        control.textColor = Color.init(hexString: "#ACB4C0")!
        
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        
        return control
    }()
    
    lazy var time_label:UILabel = {
        
        let control = UILabel()
        
        control.textColor = Color.init(hexString: "#ACB4C0")!
        
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)

        
        return control
    }()
    
    
    lazy var un_read_count_label:UILabel = {
        
        let control = UILabel()
        
        control.textColor = Color.white
        
        control.textAlignment = .center
        
        control.backgroundColor = Color.init(hexString: "#EB3C3C")
        
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        
        control.cornerRadius = 18/2.0
        
        return control
    }()
    
    
    
    fileprivate lazy var sweet_sign_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    /*
    fileprivate lazy var sign_image_container:ST_UserSignView = {
        let control = ST_UserSignView()
        return control
    }()*/
    
    fileprivate lazy var online_sub_label:ST_OnLineStatusControl = {
        return ST_OnLineStatusControl()
    }()
    
    
    fileprivate lazy var nobility_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        //v.cornerRadius = 5
        return v
    }()
    
    fileprivate lazy var conversation_guide_image_view:UIImageView = {
        let control = UIImageView()
              return control
          }()
    
    
    fileprivate lazy var normal_attributed: [NSAttributedString.Key : Any] = {
    
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.38, green: 0.4, blue: 0.44, alpha: 1)]
        
        return attr
    }()
    
    fileprivate lazy var high_line_attributed: [NSAttributedString.Key : Any] = {
    
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor.init(hex: 0xEB3C3C)!]
        
        return attr
    }()
    
//    MARK: - bind data
    private var nickname:String? = nil
    private var isGoddessOfStyle = false
    
    weak var sessionModel:ST_TianSessionSummaryStruct?
    
    func bind(model:ST_TianSessionSummaryStruct){
        
        sessionModel = model
        
        time_label.text = model.timeString
        
        let count = model.unReadCount > 99 ? "99+" : model.unReadCount.string
        
        un_read_count_label.text = count
        
        un_read_count_label.isHidden = model.unReadCount < 1
        
        last_msg_label.attributedText = model.lastMessage
        
        
        
        let provider = BINP
        
        guard let info = try? provider.storage?.object(forKey: model.sessionId) else{
            
            sweet_sign_image_view.image = nil
            
            online_sub_label.alpha = 0
            
            user_image_view.image = ST_SwiftConst.shared.fetch_random_place_holder_image()
            
            icon_supplement_image_view.image = nil
            
            bind(nickname: "加载中")
            
            conversation_guide_image_view.image = nil
            
            return
        }
            
        if let url = info.sweetValueImage?.compress_image().url{
        
            sweet_sign_image_view.sd_setImage(with: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image(), options: SDWebImageOptions(), context: nil)
            
        }else{
            
            sweet_sign_image_view.image = nil
        }
            
        if !SYSTEM_SESSION_SET.contains(model.sessionId){
            online_sub_label.bind(text: info.online_state, isSelected: info.online_state.count > 1)
            online_sub_label.alpha = 1
        }else{
            online_sub_label.alpha = 0
        }
        
        user_image_view.sd_setImage(with: info.headPic.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image(), options: SDWebImageOptions(), context: nil)
        
        if let img = info.nobilityImg,img.count > 1{
            nobility_image_view.sd_setImage(with: img.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image(), options: SDWebImageOptions(), context: nil)
        }else{
            nobility_image_view.image = nil
        }
        
        
        icon_supplement_image_view.pin_setImage(from: AVATAR_SUPPLEMENT(guardTime: info.guardTime, guardContent: info.guardContent)?.url)
        
        let nickname:String
        if info.remark.count > 0{
            nickname = info.remark
        }else{
            nickname = info.userName
        }
        
        bind(nickname: nickname, isGoddessOfStyle: info.isGoddess)
        
        
//        不是置顶聊天
        if !SYSTEM_SESSION_SET.contains(model.sessionId) && model.guardTop == false{
            
            conversation_guide_image_view.image = nil
            
        }else{
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male && model.guardTop{
                conversation_guide_image_view.image = ST_ImageUtils.imageName("conversationGuideIcon")
            }else{
                conversation_guide_image_view.image = nil
            }
        }
        
    }
    
    
    
}



extension ST_ConversationListCell{
    
    /// 女神昵称单独处理
    func bind(nickname:String,isGoddessOfStyle:Bool = false){

        if nickname == self.nickname && isGoddessOfStyle == self.isGoddessOfStyle{
            return
        }
        
        self.nickname = nickname
        self.isGoddessOfStyle = isGoddessOfStyle
        
        nickname_label.text = nickname
        
        
//        需求歧义，已删除
//        if isGoddessOfStyle{
//            goddessNicknameLabel?.removeFromSuperview()
//            nickname_label.alpha = 0
//            let goddessLabel = ST_ColoryLabel(text: nickname.withAttributes([.font(.systemFont(ofSize: 18, weight: .semibold))]), frame: CGRect(x: 83, y: 15, width: 138, height: 25), colors: [UIColor(hex:0xF89393)!,UIColor(hex:0xF152AD)!])
//            contentView.addSubview(goddessLabel)
//            goddessNicknameLabel = goddessLabel
//        }else{
//            nickname_label.alpha = 1
//            goddessNicknameLabel?.removeFromSuperview()
//        }
        
        
    }
    
}
